/* $Header: clockutil.c,v 2.2 01/10/02 10:26:01 przybyls Exp $ */

/***********************************************************************/
/*                                                                     */
/*                  Copyright Hewlett-Packard Co. 1991                 */
/*                                                                     */
/***********************************************************************/

/***********************************************************************/
/* MODULE NAME: clockutil.c                                            */
/* ============                                                        */
/*                                                                     */
/* Author: Tom Przybylski                                              */
/*                                                                     */
/* MODULE PURPOSE: Execution utilities for the clock table.            */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/***********************************************************************/


/*****************************/
/*   GLOBAL INCLUDE FILES    */
/*****************************/

#include  <typedefs.h>
#include  <error.h>
#include  <p_wkfile.h>
#include  <wkfile.h>
#include  <ramps.h>
#include  <zone_config.h>
#include  <config.h>
#include  <zone_status.h>
#include  <status.h>
#include  <run_ui.h>
#include  <sig_types.h>
#include  <sigpath.h>
#include  <clock_ui.h>
#include  <p_status.h>
#include  <det_ui.h>
#include  <valve_ui.h>
#include  <method.h>
#include  <display.h>
#include  <messages.h>
#include  <sequence.h>
#include  <seq_ui.h>
#include  <kbd_ui.h>


/*****************************/
/*   LOCAL INCLUDE FILES     */
/*****************************/


/*****************************/
/*   GLOBAL VARIABLES        */
/*****************************/


/*****************************/
/*   EXTERNAL VARIABLES      */
/*****************************/


/*****************************/
/*   LOCAL DEFINES           */
/*****************************/


/*****************************/
/*   STATIC VARIABLES        */
/*****************************/


/*****************************/
/*   IMPLEMENT SECTION       */
/*****************************/


/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/

void CTMultiValve (INT32 parm)
{
   if (Multi_Valve == 0)
   {
      CTErrorAnnotate (MSG_NO_MULTI_ERR, parm);
   }
   else
   {
      (void) SetValveStreamNo (parm);
      CTAnnotate (MSG_MULTI_ANNOTATE, parm);
   }
}


/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/

void CTBlankRun (INT32 parm)
{
   BlankRun();              /* No errors reported */

   CTAnnotate (MSG_BLANK_RUN_ANNOTATE, parm);  /* parm unused in msg */
}


/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/

void CTStartSeq (INT32 parm)
{
   if ((!Controlled_By_Host) && SeqNotLocked())
   {
      /* NOTE: Can't have seq start during a sequence because */
      /* clock table is suspended during a sequence           */
      if (StartSequence() == OK)
      {
         CTAnnotate (MSG_START_SEQ_ANNOTATE, parm);  /* parm unused in msg */
      }
      else
      {
         CTErrorAnnotate (MSG_SEQ_IS_EMPTY, parm);   /* parm unused in msg */
      }
   }
   else
   {
      /* INET or Host seq control */
      CTErrorAnnotate (MSG_SEQ_HOST_ERR, parm);   /* parm unused in msg */
   }
}


/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/

void CTPrepRun (INT32 parm)
{
   Prepare(Auto);
   CTAnnotate (MSG_PREP_RUN_ANNOTATE, parm);  /* parm unused in msg */
}


/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/

static void CTCompError (UI_ERR err)
{
   U_INT32  msg;
   INT32    num;

   num = 0;

   switch (err)
   {
   case NOT_INSTALLED:
      msg = MSG_SIG_DET_MISSING;
      break;

   default:                         /* should NEVER happen, clock table not */
   case NOT_VALID_DURING_RUN:       /* active during run                    */
   case NOT_VALID_DURING_SCC_RUN:
      msg = MSG_NOT_ALLOWED_IN_RUN;
      break;

   case SCC_RUN_LENGTH_TOO_SHORT:
      num = MIN_SCC_RUN_TIME;
      msg = MSG_CT_SCC_RUN_TOO_SHORT;
      break;

   case SCC1_DET_SETPT:
      num = 1;
      msg = MSG_NO_DET_DEFINED;
      break;

   case SCC2_DET_SETPT:
      num = 2;
      msg = MSG_NO_DET_DEFINED;
      break;

   case FRONT_DET_OFF:
      msg = MSG_FRONT_DET_OFF;
      break;

   case BACK_DET_OFF:
      msg = MSG_BACK_DET_OFF;
      break;
   }

   CTErrorAnnotate (msg, num);
}


void CTColComp1 (INT32 parm)
{
   UI_ERR  err;

   if ((err = StartBlankRun (CCOMP1, TRUE)) != OK)
   {
      CTCompError (err);
   }
   else
   {
      CTAnnotate (MSG_CCOMP_ANNOTATE, 1);
   }

   (void) parm;
}


void CTColComp2 (INT32 parm)
{
   UI_ERR  err;

   if ((err = StartBlankRun (CCOMP2, TRUE)) != OK)
   {
      CTCompError (err);
   }
   else
   {
      CTAnnotate (MSG_CCOMP_ANNOTATE, 2);
   }

   (void) parm;
}


void CTColCompBoth (INT32 parm)
{
   UI_ERR  err;

   if ((err = StartBlankRun (CCOMP1_2, TRUE)) != OK)
   {
      CTCompError (err);
   }
   else
   {
      CTAnnotate (MSG_CCOMP_BOTH_ANNOTATE, parm);  /* parm unused in msg */
   }
}


/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/

void CTLoadMethod (INT32 method)
{
   if (MethodExists((U_INT8)method) == OK)
   {
      if (CheckMethod((U_INT8) method) == OK)
      {
         CTAnnotate (MSG_METHOD_ANNOTATE, method);
      }
      else
      {
         CTErrorAnnotate (MSG_CT_METHOD_CONFIG, method);
      }

      /* Load even if config errors: */
      (void) LoadMethod ((U_INT8)method);
   }
   else
   {
      CTErrorAnnotate (MSG_NO_METHOD, method);
   }
}


/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/

void CTLoadSequence (INT32 sequence)
{
   if (SequenceExists((U_INT8)sequence) == OK)
   {
      (void) LoadSequence((U_INT8) sequence);  /* ignores "garbage" memory errors */
                                               /* like keyboard                   */
      CTAnnotate (MSG_SEQ_LOAD_ANNOTATE, sequence);
   }
   else
   {
      CTErrorAnnotate (MSG_EMPTY_SEQ, sequence);
   }
}


/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/

void CTDetOffsetSeek (INT32 parm)
{
   switch (GetDetectorType((POSITION) parm))
   {
   case ECD:
      (void) StartECDOffsetSeek((POSITION) parm);
      CTAnnotate (MSG_DET_ADJUST_ANNOTATE, parm);
      break;

   case NPD:
      if ((StartNPDOffsetSeek((POSITION) parm)) != OK)
      {
         CTErrorAnnotate (MSG_DET_ADJUST_ERR, parm);
      }
      else
      {
         CTAnnotate (MSG_DET_ADJUST_ANNOTATE, parm);
      }
      break;

   default:
      CTErrorAnnotate (MSG_ADJUST_WRONG_DET_ERR, parm);
      break;
   }
}


/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/

void CTAnnotate (U_INT32 msg, INT32 parm)
{
   DisplayTwoRomMessage (MSG_CLOCK_ANNOTATE, msg, (INT32) DateTime.hour,
                         (INT32) DateTime.minute, parm);
}

void CTErrorAnnotate (U_INT32 msg, INT32 parm)
{
   DisplayTwoRomMessage (MSG_ABORTED_CT_EVENT, msg, (INT32) DateTime.hour,
                         (INT32) DateTime.minute, parm);
}


/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/

/* note: the clock table drivers for valves are over in the valve  */
/*       directory in valve_ctrl.c                                 */

void CTValveAnnotate (U_INT8 valve, INT32 parm)
{
   U_INT32 msg;

   if ((ON_OFF) parm == ON)
   {
      msg = MSG_VALVE_ON_ANNOTATE;
   }
   else
   {
      msg = MSG_VALVE_OFF_ANNOTATE;
   }

   CTAnnotate (msg, (INT32)valve);
}

